<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Exception;
use Illuminate\Support\Facades\Session;

class SupplyerController extends Controller {


       public function list(){
            if(Session::get('role_id') == 1 ) {

                $supplyer = DB::table('supplyer_company')
                    ->select('*')
                    ->orderBy('id','desc')
                    ->paginate(10);

            }else if(Session::get('role_id') == 2 ){

                $supplyer = DB::table('supplyer_company')
                    ->select('*')
                    ->where('company_id',Session::get('session_id'))
                    ->orderBy('id','desc')
                    ->paginate(10);

            }
            return view('admin.supplyer.list',['supplyer'=>$supplyer]);
       }


       public function add(){

            $role_id = Session::get('role_id');
            $session_id = Session::get('session_id');

            if($role_id == 2){
                $session_role_name = Session::get('company_name');
                return view('admin.supplyer.add',['company_id'=>$session_id,'company_name'=>$session_role_name]);
            }else{
                echo "<script language='javascript'>alert('进入失败，必须公司账号');window.location.href='/admin/supplyer/list';</script>";
            }
       }


       /*
        * 添加处理
        * */
       public function add_do(Request $request){
            $supplyer = $request->all();
            unset($supplyer['_token']);
            foreach ($supplyer as $key =>$value){
                if(empty($value)){
                    echo "<script language='javascript'>alert('添加失败,不能为空');window.location.href='/admin/supplyer/add';</script>";
                    exit;
                }
            }
            $supplyer['company_name'] = Session::get('company_name');
            $supplyer['create_time'] = time();
            $supplyer['create_date'] = date('Y-m-d H:i:s',time());

            $bool = DB::table('supplyer_company')->insert($supplyer);

            if($bool){
                echo "<script language='javascript'>alert('添加成功');window.location.href='/admin/supplyer/list';</script>";
            }else{
                echo "<script language='javascript'>alert('添加失败');window.location.href='/admin/supplyer/list';</script>";
            }
       }


       /*
        *查看记录
        * */
       public function log(Request $request){

           if(Session::get('role_id') == 1 ) {
               $servicer = DB::table('supplyer_company')
               ->select('name')
               ->get();

           }else if(Session::get('role_id') == 2 ){
                $servicer = DB::table('supplyer_company')
               ->select('name')
               ->where('company_id',Session::get('session_id'))->get();
            }



           $servicer_id = [];
           foreach ($servicer as $key=>$value){
               $servicer_id[] = $value->name;
           }

           $order = DB::table('order')
               ->select('order_product_id','driver_id','driver_name','driver_get_date','arriver_date','supplyer_company','total_sell')
               ->where('driver_status','>=',1)
               ->whereIn('supplyer_company',$servicer_id)
               ->get();

           $order_product_id = [];
           $driver_info = [];
           foreach ($order as $key=>$value){
              $order_product_id[] = explode(',',$value->order_product_id);
              $arr = explode(',',$value->order_product_id);
              foreach ($arr as $k=>$v){
               $driver_info[$v]['driver_id'] = $value->driver_id;
               $driver_info[$v]['driver_name'] = $value->driver_name;
               $driver_info[$v]['driver_get_date'] = $value->driver_get_date;
               $driver_info[$v]['arriver_date'] = $value->arriver_date;
               $driver_info[$v]['supplyer_company'] = $value->supplyer_company;
               $driver_info[$v]['total_sell'] = $value->total_sell;
              }
           }
           $arr_id = [];
           foreach ($order_product_id as $key=>$value){
             $arr_id = array_merge($arr_id,$value);
           }

           $order_product = DB::table('order_product')
               ->select('*')
               ->whereIn('id',$arr_id)->get();

           $order_product = json_decode($order_product,1);
           foreach ($order_product as $key=>$value){
              $order_product[$key]['driver_id'] = $driver_info[$value['id']]['driver_id'];
              $order_product[$key]['driver_name'] = $driver_info[$value['id']]['driver_name'];
              $order_product[$key]['driver_get_date'] = $driver_info[$value['id']]['driver_get_date'];
              $order_product[$key]['arriver_date'] = $driver_info[$value['id']]['arriver_date'];
              $order_product[$key]['supplyer_company'] = $driver_info[$value['id']]['supplyer_company'];
              $order_product[$key]['total_sell'] = $driver_info[$value['id']]['total_sell'];
           }
            $order_product  = (object)$order_product;
           return view('admin/supplyer/log',['product'=>$order_product]);

       }

       /*
        * 图形分析
        * */
       public function general(){

            if(Session::get('role_id') == 1 ) {
               $servicer = DB::table('supplyer_company')
               ->select('name')
               ->get();
           }else if(Session::get('role_id') == 2 ){
               $servicer = DB::table('supplyer_company')
               ->select('name')
               ->where('company_id',Session::get('session_id'))->get();
            }

           $servicer_id = [];
           foreach ($servicer as $key=>$value){
               $servicer_id[] = $value->name;
           }

           $order = DB::table('order')
               ->select('supplyer_company','product_num','total_sell')
               ->where('driver_status','>=',1)
               ->whereIn('supplyer_company',$servicer_id)
               ->get();
            $output_num = [];
            $servicer_num = [];
            $number = 0;
            $barData = [];
            $barData['label'] ="bar";
            $i = 1;
           foreach ($order as $key=>$value){
               $barData['data'][] = [$i,$value->total_sell];
               $i++;
               $number += $value->product_num;
               if(!isset($servicer_num[$value->supplyer_company])){
                    $servicer_num[$value->supplyer_company]['data'] = $value->product_num;
                    $servicer_num[$value->supplyer_company]['label'] = $value->supplyer_company;
                    $servicer_num[$value->supplyer_company]['color'] = '#'.rand(111000,999999);
               }else{
                    $servicer_num[$value->supplyer_company]['data'] += $value->product_num;
               }
           }

           foreach ($servicer_num as $key=>$value){
               $output_num[] = $value;
           }
           return view('admin/supplyer/general',['output_num'=>$output_num,'number'=>$number,'barData'=>$barData]);
       }




}